package com.kabigon.multiverse.common.api;

import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * 通用返回对象
 *
 * @author xu.biao
 * @version v1.0
 * @Package com.kabigon.multiverse.common
 * @data 2022/2/17 20:45
 * @date 2022/02/17
 */
@Data
public class ResultData<T> {

    private String message;
    private int status;
    private T data;
    private long timestamp;

    public ResultData() {
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 成功返回结果
     *
     * @param data
     * @param <T>
     * @return ResultData
     */
    public static <T> ResultData<T> success(T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setMessage(HttpStatus.OK.getReasonPhrase());
        resultData.setStatus(HttpStatus.OK.value());
        resultData.setData(data);
        return resultData;
    }

    /**
     * 失败返回结果
     *
     * @param httpStatus
     * @param <T>
     * @return ResultData
     */
    public static <T> ResultData<T> fail(HttpStatus httpStatus) {
        return fail(httpStatus.value(), httpStatus.getReasonPhrase());
    }

    public static <T> ResultData<T> fail(int status, String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setMessage(message);
        resultData.setStatus(status);
        return resultData;
    }

}
